package net.yla.smartbus.core.util.vc;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;

/**
 * @author : yang lei
 * @version : 1.0
 * @date : Created in 9:54 2020/10/30
 * @description :
 */
public class BaiduMapUtil {

    public static JSONObject getAddr(String lat, String lng) throws IOException {

        String url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=0MA953dkYQM4fQriWe6NaII8QpjeCCZh&output=json&coordtype=wgs84ll&location=" + lat + "," + lng;
        String json = loadJSON(url);
        if (json != null && !"".equals(json)) {
            JSONObject obj = JSONObject.parseObject(json);
            if ("0".equals(obj.getString("status"))) {
                return obj;
            }
        }
        return null;
    }

    public static String loadJSON(String value) {
        StringBuilder json = new StringBuilder();
        try {
            URL url = new URL(value);
            URLConnection connection = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json.toString();
    }

}
